<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/2/28 0028
 * Time: 10:00
 */
include "config.php";
if ($_SERVER["REQUEST_METHOD"] == "POST"){
    $query = " SELECT*FROM `cartinfo` WHERE `uid`='" . $_POST["uId"] . "' AND `gId`='" . $_POST["gId"] . "'";

    $resultA = $conn->query($query);
    if ($resultA->num_rows >= 1) {
        // 存在 修改数量

        $update = " UPDATE `cartinfo` SET num=num+" . $_POST["num"] . " , uTotal=gPrice*num   WHERE `uId`='" . $_POST["uId"] . "' AND `gId`='" . $_POST["gId"] . "'";

        $resultB = $conn->query($update);
        if ($resultB) {
            print_r(json_encode(array("msg" => "加入成功", "status" => 1)));
        } else {
            print_r(json_encode(array("msg" => "加入失败", "status" => -1)));
        }
    } else {
        //不存在, 添加记录
        $insert = " INSERT INTO `cartinfo` (`gId`,`gName`,`gBrand`,`gPrice`,`gImg`,`uid`,`num`,`uTotal`)
            VALUES('" . $_POST["gId"] . "','" . $_POST["gName"] . "','" . $_POST["gBrand"] . "','" . $_POST["gPrice"] . "','" . $_POST["gImg"] . "','" . $_POST["uId"] . "','" . $_POST["num"] . "','".$_POST["num"]*$_POST["gPrice"]."')";
        $resultC = $conn->query($insert);
        if ($resultC) {
            print_r(json_encode(array("msg" => "加入成功", "status" => 2)));
        } else {
            print_r(json_encode(array("msg" => "加入失败", "status" => -2)));
        }
    }
}else{
    $arr = array("meg"=>"不支持GET请求","status"=>-3);
    print_r(json_encode($arr));
}